package lms.dwr.demo.dao.impl;

import java.util.List;

import lms.dwr.demo.common.DAOException;
import lms.dwr.demo.dao.DAOUser;
import lms.dwr.demo.model.ModelUser;
import lms.dwr.demo.utils.UtilCipher;

import org.apache.log4j.Logger;

/**
 * The implementation of account repository.
 * 
 * @author Jeccy.Zhao
 *
 */
@SuppressWarnings("unchecked")
public class DAOUserImpl 
extends DAOBaseImpl<ModelUser> implements DAOUser
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(DAOUserImpl.class);
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.dao.AccountDAO#getAccountByNameAndPwd(java.lang.String, java.lang.String)
	 */
	public ModelUser getAccountByNameAndPwd(String userName, 
			String password) throws DAOException 
	{
		
		List<ModelUser> result = findListByNativeSQL(
				"SELECT * FROM dwrlms_users WHERE LOWER(username) = '" + 
					userName.toLowerCase() + "' AND password = '" + 
					UtilCipher.generatePassword(password) + "'", ModelUser.class);
		
		return result.size() > 0 ? result.get(0) : null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.lzcp.dao.AccountDAO#getAccountByName(java.lang.String)
	 */
	public ModelUser getAccountByName(String userName) 
			throws DAOException
	{
		List<ModelUser> result = findListByNativeSQL(
					"SELECT * FROM dwrlms_users WHERE LOWER(username) = '" + 
						userName.toLowerCase() + "'", ModelUser.class);
		
		return result.size() > 0 ? result.get(0) : null;
	}

	public static Logger getLogger()
	{
		return LOGGER;
	}

}
